package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.ex.BusinessException;
import com.itheima.ex.SystemException;
import com.itheima.pojo.Book;
import com.itheima.service.BookService;
import com.itheima.vo.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author by itheima
 * @Date 2022/4/27
 * @Description 定义操纵book的服务bean
 */
@Service("bookService")
//@Transactional
public class BookServiceImpl implements BookService {
    /**
     * 注入bookDao bean
     */
    @Autowired
    private BookDao bookDao;

    //@Transactional
    @Override
    public boolean save(Book book) {
        int flag = bookDao.save(book);
        return flag>0;
    }

    @Override
    public boolean update(Book book) {
        int flag = bookDao.update(book);
        return flag>0;
    }

    @Override
    public boolean delete(Integer id) {
        int flag = bookDao.delete(id);
        return flag>0;
    }

    @Override
    public Book getById(Integer id) {
        //用户非法传入错误的数据导致的--》业务异常
        if (id<0){
            throw new BusinessException(Code.BUSINESS_ERR,"当前数据不能小于0");
        }
        Book book =null;
        try {
           book= bookDao.getById(id);
//           int a=10/0;//模拟系统异常
        } catch (Exception e) {
//            e.printStackTrace();
            //模拟数据库连接异常
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"数据库连接超时");
        }
        int a=10/0;//模拟未知异常
        return book;
    }

    @Override
    public List<Book> getAll() {
        return bookDao.getAll();
    }
}
